#coding=utf-8
import urllib,re,datetime

def md5(s):
  import hashlib
  m = hashlib.md5(s)
  m.digest()
  return m.hexdigest()
  
re_title= r'<title>.*?</title>'
re_desc = r'<meta.*description.*?>'
re_keywords = r'<meta.*keywords.*?>'

re_charset = r'<meta.*charset=(.*?)>'
re_bookmark = r'<a.*?class="tofavorite".*?</a>'
re_tongji = r'<script.*?</noscript>'

url ='http://pindao.huoban.taobao.com/channel/%s'
re_h = r'href="%s.*?"'

html_nav = (
  ('channel_code.htm\?pid=mm_12837376_0_0', '/zhonghe.html'),
  ('channel_mall.htm\?pid=mm_12837376_0_0', '/mall.html'),
  ('lady.htm\?pid=mm_12837376_0_0', '/lady.html'),
  ('beauty.htm\?pid=mm_12837376_0_0', '/beauty.html'),
  ('jewelry.htm\?pid=mm_12837376_0_0', '/jewlry.html'),
  ('man.htm\?pid=mm_12837376_0_0', '/man.html'),
  ('baby.htm\?pid=mm_12837376_0_0', '/baby.html'),
  ('digital.htm\?pid=mm_12837376_0_0', '/digital.html'),
  ('brand_lib.htm\?pid=mm_12837376_0_0', '/brand.html'),
  ('shop_street.htm\?pid=mm_12837376_0_0', '/shops.html'),
)

def get_pindao_html(url):
  return urllib.urlopen(url).read()

def decode_html(html_content):
  #字符编码
  h = html_content
  m = re.search(re_charset, h, re.IGNORECASE)
  if m:
    charset_html = m.group()
    codec = m.group(1)
    m = re.search(r'\w+\b', codec, re.IGNORECASE)
    codec = m.group()
    h = h.decode(codec, 'ignore').encode('utf-8', 'ignore')  
    h = h.replace(charset_html, '<meta http-equiv="Content-Type" content="text/html; charset=utf-8">')
  else:
    codec = 'GB18030'  
    h = h.decode(codec, 'ignore').encode('utf-8', 'ignore')  
    m = re.search(r'<head>', h, re.IGNORECASE)
    s1, e1 = m.start(), m.end()
    h = h[:e1] + '\n<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />\n' + h[e1:]
  
  return h

def process_html(html_content):
  h = html_content
  #去掉收藏
  h = re.sub(re_bookmark, '', h)
  #去掉统计代码
  h = re.sub(re_tongji, '', h)
  #改变导航
  for n in html_nav:
    a, b = n
    h = re.sub(r'href="%s.*?"'%a, 'href="%s"'%b, h)
  return h

def update_title(html_content, title='test'):
  h = html_content
  title = '<title>%s</title>'%title
  
  m = re.search(re_title, h, flags=re.IGNORECASE)
  t = m.group()
  h = h.replace(t, title)
  #print 'title updated'
  return h
  
def update_desc(html_content, desc='test'):
  h = html_content
  desc = '<meta name="description" content="%s"/>'%desc

  m = re.search(re_desc, h, flags=re.IGNORECASE)
  if m:
    d = m.group()
    h = h.replace(d, desc)
  else:
    print 'no desc'
    m = re.search(re_title, h, flags=re.IGNORECASE)
    n = m.end()
    h = h[:n] + '\n%s\n'%desc + h[n:]
  #print 'desc updated'
  return h
  
def update_keywords(html_content, keywords='test'):
  h = html_content
  keywords = '<meta name="keywords" content="%s"/>'%keywords
  m = re.search(re_keywords, h, flags=re.IGNORECASE)
  
  if m:
    k = m.group()
    h = h.replace(k, keywords)
  else:
    print 'no_keywords'
    m = re.search(re_desc, h, flags=re.IGNORECASE)
    n = m.end()
    h = h[:n] + '\n%s\n'%keywords + h[n:]
  #print 'keywords updated'
  return h
    
pdir = './template'
f_header = 'topheader.html'
f_links = 'links.html'
f_footer = 'footer.html'

def render_template(html_content):
  h = html_content
  
  m = re.search(r'</head>', h, re.IGNORECASE)
  s1, e1 = m.start(), m.end()
  
  m = re.search(r'<body>', h, re.IGNORECASE)

  if m:
    s2, e2 = m.start(), m.end()
    m = re.search(r'</body>', h, re.IGNORECASE)
    s3 = m.start()
  else:
    e2 = e1
    s3 = len(h)
    
  css = '<link rel="stylesheet" type="text/css" href="/css/fdd.css" />\n'
  js_jquery = ''#'<script type="text/javascript" src=%s"></script>\n'%'http://code.jquery.com/jquery-1.4.1.min.js'
  
  header = open('%s/%s'%(pdir, f_header)).read()
  links = open('%s/%s'%(pdir, f_links)).read()
  footer = open('%s/%s'%(pdir, f_footer)).read()
  
  content = h[:s1] + css + js_jquery + h[s1:e2] + header + h[e2:s3] + links + footer + h[s3:]
  return content

def process_pindao_html(html_content, pindao='', is_decoded=False):
  h = html_content
  if not is_decoded:
    #print 'decoding'
    h = decode_html(h)
  h = process_html(h)
  if pindao:
    h = update_title(h, title=pindao.title.encode('u8'))
    h = update_desc(h, desc=pindao.des.encode('u8'))
    h = update_keywords(h, keywords=pindao.keywords.encode('u8'))
  else:
    h = update_title(h)
    h = update_desc(h)
    h = update_keywords(h)
  h = render_template(h)
  return h
